#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <netdb.h>
#include "rbtree.h"
#ifndef DNS_HEADER_INCLUDED
#define DNS_HEADER_INCLUDED

enum dns_query_type {DNS_A_RECORD = 0x01, DNS_MX_RECORD = 0x0f};


#define	DNS_QUERY_TIMEOUT	30	/* Query timeout, seconds */

typedef void (*dns_callback_t)(void *context, const char *name, const unsigned char *addr, size_t addrlen);

struct dns 
{
	int		            sock;      // UDP socket used for queries 
	struct sockaddr_in	sa;	       // DNS server socket address 
	uint16_t	        tid;	   // Latest tid used 
	
	dns_callback_t	    callback;
	struct rb_root      active;
};

extern struct dns *dns_init(dns_callback_t callback);

extern void	dns_poll(void *context, struct dns *dns);

int dns_get_fd(struct dns *dns);

void dns_request(struct dns *dns, const char *name, enum dns_query_type qtype);

#endif 
